package com.lzx.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.lzx.api.CommonResult;
import com.lzx.api.IErrorCode;
import com.lzx.api.ResultCode;
import com.lzx.dao.repository.UserRepository;
import com.lzx.dto.OrderDto;
import com.lzx.dto.OrderItemDto;
import com.lzx.dto.RegisterDto;
import com.lzx.dto.UserDto;
import com.lzx.entity.Brand;
import com.lzx.entity.Member;
import com.lzx.entity.Order;
import com.lzx.entity.Orderitem;
import com.lzx.service.*;
import com.lzx.util.PageUtil;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * (Member)表控制层
 *
 * @author makejava
 * @since 2021-02-22 19:40:14
 */
@RestController
@RequestMapping("/users")
public class MemberController {
    /**
     * 服务对象
     */
    @Resource
    private MemberService memberService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderitemService orderitemService;
    @Autowired
    private ProductService productService;
    @Autowired
    private  PictrueService pictrueService;
    @Autowired
    private UserRepository userRepository;


    @RequestMapping("/login")
    public CommonResult<Member> login(@RequestBody Map<String,String> map) {
        String username = map.get("username");
        String password = map.get("password");
        Member member = memberService.login(username, password);
        if(BeanUtil.isNotEmpty(member)){
            return CommonResult.success(member);
        }else {
            return CommonResult.failed();
        }
    }

    @RequestMapping("/register")
    public CommonResult<String> region(@RequestBody RegisterDto register){
        Boolean result = memberService.register(register);
        if(result) {
            return CommonResult.success(ResultCode.SUCCESS.getCode(), "注册成功！");
        }
        return CommonResult.failed(ResultCode.FAILED);
    }
    @RequestMapping("/findUserName")
    public CommonResult<String> findUserName(@RequestBody Map<String,String> map){
        System.out.println(map.get("username"));
        Member member = memberService.getOne(new QueryWrapper<Member>().eq("username",map.get("username")));
        if(member!=null){
            return CommonResult.failed("用户已存在");
        }else {
            return CommonResult.success(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMsg());
        }
    }
    @GetMapping("/listByPage")
    public CommonResult listByPage(@RequestParam Integer pagenum,@RequestParam Integer pagesize, @RequestParam(value = "search", required = false) String search){
        memberService.dbToEs();
        List<UserDto> userList=new ArrayList<>();
        if(BeanUtil.isEmpty(search)){
            userRepository.findAll().forEach(userList::add);
        }else {
            userList.addAll(userRepository.queryByUsername(search));
        }

        PageInfo<UserDto> pageInfo = PageUtil.startPage(userList,pagenum, pagesize);
        return CommonResult.success(pageInfo);
    }
    @RequestMapping("/orderList")
    public CommonResult<List<OrderDto>> listOrder(Integer memberId){
        List<Order> orderList = orderService.list(new QueryWrapper<Order>().eq("memberid", memberId));
        List<OrderDto> resultList = new ArrayList<>();
        for (Order order : orderList) {
            List<OrderItemDto> orderItemDtos = new ArrayList<>();
            OrderDto orderDto = new OrderDto();
            orderDto.setId(order.getId());
            orderDto.setCreatetime(order.getCreatetime());
            orderDto.setState(order.getState());
            List<Orderitem> orderitems = orderitemService.list(new QueryWrapper<Orderitem>().eq("orderid", order.getId()));
            orderitems.forEach(orderitem -> {
                OrderItemDto orderItemDto = new OrderItemDto();
                orderItemDto.setCount(orderitem.getCount());
                orderItemDto.setPicUrl(pictrueService.getDefaultImg("product",orderitem.getProductid()));
                orderItemDto.setProduct(productService.getById(orderitem.getProductid()));
                orderItemDtos.add(orderItemDto);
            });
            orderDto.setOrderItemList(orderItemDtos);
            resultList.add(orderDto);
        }
        return CommonResult.success(resultList);
    }
    @PutMapping("/changeStatus")
    public CommonResult changeStatus(Integer id, Boolean status) {
        Boolean result = memberService.changeStatus(id, status);
        if (result) {
            return CommonResult.success(true);
        }else {
            return CommonResult.failed();
        }
    }
}
